﻿using System;
using UnityEngine;

namespace GameToolkit
{
    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class MaskAttribute : PropertyAttribute
    {
        public int ValidMask { get; set; }
        public System.Type EnumType { get; set; }
        public bool IsIndexEnum { get; set; }
        public int ToggleColumns { get; set; }
        public bool AsEnums { get; set; }
        public MaskAttribute(Type enumType) { EnumType = enumType; IsIndexEnum = false; ValidMask = -1; }
    }


    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class SortingLayerFieldAttribute : PropertyAttribute
    {
    }

    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class LayerFieldAttribute : PropertyAttribute
    {

    }

    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class RangeFieldAttribute : PropertyAttribute
    {
        public float Min { get; set; }
        public float Max { get; set; }
        public bool SliderMode { get; set; }

        public RangeFieldAttribute() : base()
        { Min = float.MinValue; Max = float.MaxValue; }

        public RangeFieldAttribute(float min, float max)
        {
            Min = min;
            Max = max;
            SliderMode = true;
        }
    }

    /// <summary>
    /// 引用表数据ID (TableFile 路径中的使用/%%fallback/自动替换为所在文件夹名字， 如果不存在该文件夹，则使用 fallback 作为默认文件夹)
    /// 注意： 属性类型仅支持 int 或者 TableReference
    /// </summary>
    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class TableReferenceAttribute : PropertyAttribute
    {
        string mFile;
        string mProperty;
        string mPropertyRef;
        public string TableFile
        {
            get { return mFile; }
            set
            {
                mFile = value == null ? "" : value.ToLower();
            }
        }
        public string TableProperty
        {
            get { return mProperty; }
            set
            {
                mProperty = value;
                if (string.IsNullOrEmpty(value))
                    mPropertyRef = null;
                else
                    mPropertyRef = $"{mProperty}.ref";
            }
        }
        public int PageSize { get; set; }
        public string TextReference => mPropertyRef;
        public TableReferenceAttribute() { }
        public TableReferenceAttribute(string uri)
        {
            var i = uri.LastIndexOf('@');
            if (i >= 0 && i < uri.Length - 1)
            {
                TableFile = uri.Substring(0, i);
                TableProperty = uri.Substring(i + 1);
            }
            else
            {
                TableFile = uri;
                TableProperty = null;
            }
        }
    }

    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class UIDAttribute : PropertyAttribute
    {
        public bool EnableUpdate { get; set; }
        public bool EnableZero { get; set; }
        public bool Hex { get; set; }
        public UIDAttribute() { EnableUpdate = false; EnableZero = false; }
        public UIDAttribute(bool enableUpdate) { EnableUpdate = enableUpdate; }
        public UIDAttribute(bool enableUpdate, bool enableZero) { EnableUpdate = enableUpdate; EnableZero = enableZero; }
    }

    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class IDByNameAttribute : PropertyAttribute
    {
        public bool IgnoreCase { get; set; }
        public bool Hex { get; set; }
        public bool IsPrefabName { get; set; }
        public IDByNameAttribute() { IgnoreCase = true; IsPrefabName = true; }
        public IDByNameAttribute(bool ignoreCase) { IgnoreCase = ignoreCase; IsPrefabName = true; }
    }


    public enum EEnableEditMode
    {
        EditorOnly,
        RuntimeOnly,
        Disabled,
        AlwaysEnabled,
    }

    [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
    public class EnableEditAttribute : PropertyAttribute
    {
        public EEnableEditMode EnableMode { get; set; }
        public EnableEditAttribute() { EnableMode = EEnableEditMode.EditorOnly; }
        public EnableEditAttribute(EEnableEditMode mode) { this.EnableMode = mode; }
    }

}
